package com.base.caselib.presenter;

import android.app.Activity;

import com.base.caselib.bean.TaskInfo;
import com.base.caselib.utils.DownLoadUtils;
import com.base.caselib.view.PowerMatterView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.SystemUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/12/10.
 */

public class PowerMatterPresenter extends BasePresenter<PowerMatterView>{
    public PowerMatterPresenter(PowerMatterView view, Activity activity) {
        super(view, activity);
    }

    DownLoadUtils downLoadUtils;
    public void downLoadFiel(String url,String name){
        String[] names = url.split("\\.");
        String fileName = name+"."+names[names.length-1];

        File file = new File(SystemUtil.getAppDir());
        if (!file.exists()){
            file.mkdirs();
        }
        File file1 = new File(SystemUtil.getAppDir()+fileName);
        if (file1.exists()){
            file1.delete();
        }
        TaskInfo info = new TaskInfo();
        info.setName(fileName);
        info.setPath(SystemUtil.getAppDir());
        info.setUrl(url);
        downLoadUtils = new DownLoadUtils(mActivity,info);
        downLoadUtils.showDownDialog();
    }

    public void getPowerPdf(String dutyPerson, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dutyPerson",dutyPerson);
        params.put("inquirer_id1",mUser.getUserId());
        params.put("inquirer_id2",mCurrentPartner.getId());
        params.put("modelId","YPijz0ywa52U8vNiomm");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforDuty")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void release() {
        super.release();
        if (downLoadUtils != null){
            downLoadUtils.onDestory();
        }
    }
}
